#include "unit.h"
#include <cmath>

Unit::Unit()
{
    base = false;
    target = nullptr;
    attacking = false;
    alive = true;
    reloadTime = 2000;
    timeToReload = 0;
}
void Unit::update(){
    if(waypoints.size() != 0){
        if(x == currentWaypoint->getX() && y == currentWaypoint->getY()){
            currentWaypoint = waypoints.front();
            waypoints.pop();
        }
    }
    if(target != NULL){
        int distanceToTarget = sqrt(pow((x - target->getX()), 2) + pow(y - target->getY(), 2));
        if(distanceToTarget > range){
            targX = target->getX();
            targY = target->getY();
        }
        else {
            targX = x;
            targY = y;
            if(timeToReload == 0){
                attack();
                timeToReload = reloadTime;
            }else {
                timeToReload--;
            }
        }
    }
    Entity::update();
}
Tank::Tank(int initX, int initY){
    x = initX;
    y = initY;
    targX = x;
    targY = y;

    attackStrength = 30;
    range = 200;
    health = 150;
    reloadTime = 20;
}
void Unit::setAttackTarget(Entity *targ){
    target = targ;
}
void Unit::attack(){
    if(!target->isAlive()){
        target = NULL;
        return;
    }
    target->damage(attackStrength);
}
Truck::Truck(int initX, int initY){
    x = initX;
    y = initY;
    targX = x;
    targY = y;

    attackStrength = 8;
    range = 30;
    health = 50;
    reloadTime = 10;
}
